<template>
  <div>
    <el-container>
      <!-- 侧边栏-->
      <!--100vh相当于整个页面的高度 background-color:  #001529 一级菜单颜色-->
      <!-- :width="asideWidth" 绑定-->
      <el-aside :width="asideWidth" style="min-height: 100vh; background-color:  #001529">
        <!-- align-items: center; 上下居中  justify-content: center  左右居中 -->
        <div style="height: 60px; color: white; display: flex; align-items: center; justify-content: center">
          <transition name="el-fade-in-linear">
            <img src="@/assets/images/h_logo.png" alt="" style="width: 100%; height: 100%" v-show="isCollapse == false">
          </transition>
          <transition name="el-fade-in-linear">
            <img src="@/assets/images/f_logo.png" alt="" style="width: 100%; height: 100%" v-show="isCollapse == true">
          </transition>
        </div>

        <!--$route.path 是指当前浏览器访问的路由  default-active  高亮-->
        <!--router 开启路由跳转-->
        <!--:collapse-transition="false" 动画不生效-->
        <el-menu :default-openeds="['info']" :collapse="isCollapse" :collapse-transition="false" router background-color="#001529"
                 text-color="rgba(255,255,255,0.65)" active-text-color="#fff"
                 style="border: none; margin-top: 10px" :default-active="$route.path">
          <el-menu-item index="/home">
            <i class="el-icon-s-home"/>
            <span slot="title">系统首页</span>
          </el-menu-item>
          <el-menu-item index="/user">
            <i class="el-icon-user"/>
            <span slot="title">用户信息管理</span>
          </el-menu-item>
          <el-menu-item index="/chargingDevice">
            <i class="el-icon-setting"/>
            <span slot="title">充电宝信息管理</span>
          </el-menu-item>
          <el-menu-item index="/deviceType">
            <i class="el-icon-s-order"/>
            <span slot="title">充电桩设备管理</span>
          </el-menu-item>
          <el-menu-item index="/address">
            <i class="el-icon-location-outline"/>
            <span slot="title">设备地址</span>
          </el-menu-item>
          <el-menu-item index="/userOrder">
            <i class="el-icon-mobile-phone"/>
            <span slot="title">用户订单</span>
          </el-menu-item>
          <el-menu-item index="/rentRecord">
            <i class="el-icon-s-promotion"/>
            <span slot="title">用户租借归还</span>
          </el-menu-item>

          <!-- 二级菜单-->
          <!-- 如果角色为管理员则显示，否则不显示 -->
          <el-submenu index="info" v-if="user.role === '管理员' ">
            <template slot="title">
              <i class="el-icon-s-tools"></i>
              <span>信息管理</span>
            </template>
            <el-menu-item index="/notice">系统公告</el-menu-item>
            <el-menu-item index="/logs">系统日志</el-menu-item>
            <el-menu-item index="/activity">活动</el-menu-item>
            <el-menu-item index="/test2">ces</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>


      <el-container>
        <!--头部区域-->
        <el-header>
          <i :class="collapseIcon" style="font-size: 26px" @click="handleCollapse"></i>
          <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-left: 20px">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: this.$route.path}">{{ this.$route.meta.name }}</el-breadcrumb-item>
          </el-breadcrumb>

          <div style="flex: 1; width: 0; display: flex; align-items: center; justify-content: flex-end">
            <i class="el-icon-quanping" style="font-size: 26px" @click="handleFull"></i>
            <el-dropdown placement="bottom">
<!--              <div style="display: flex; align-items: center; justify-content: center">-->
<!--                <el-avatar :size="32" :src="user.avatar" style="margin: 0 5px"></el-avatar>-->
<!--                <span>{{this.user.name}}</span>-->
<!--              </div>-->
              <div style="display: flex; align-items: center; cursor: default">
                <img :src="user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" style="width: 40px;height: 40px;border-radius: 50%;margin: 0 5px">
                <span>{{this.user.name}}</span>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="$router.push('/person')">个人信息</el-dropdown-item>
                <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>

        <!--        主体区域-->
        <el-main>
          <router-view @update:user="updateUser"/>
        </el-main>

        <!--底部区域-->
        <!--        <el-footer>底部区域</el-footer>-->
      </el-container>
    </el-container>
  </div>
</template>

<script>
// 引用本地图片
import circleUrl from "@/assets/images/f_logo.png";

export default {
  name: 'HomeView',
  data() {
    return {
      isCollapse: false,  // 不收缩
      asideWidth: "200px",  // 展开状态默认200x
      collapseIcon: 'el-icon-s-fold', // 收缩图标的绑定
      circleUrl: circleUrl,
      user: JSON.parse(localStorage.getItem('power-user') || '{}'),

    };
  },
  mounted(){
    // console.log(this.$route)
  },
  methods: {
    updateUser(user){
      this.user = JSON.parse(JSON.stringify(user)) // 让父级的对象跟子级的对象毫无关联
    },
    logout(){
      localStorage.removeItem("power-user")  // 清除当前用户存储的数据
      this.$router.push('/login')
    },
    handleFull() {
      // 实现全屏
      document.documentElement.requestFullscreen();
    },
    handleCollapse() {
      // 取反
      this.isCollapse = !this.isCollapse
      // 通过true或false来得出要赋值
      this.asideWidth = this.isCollapse ? '64px' : '200px'
      this.collapseIcon = this.isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'
    }
  }
}
</script>


<style>
/*二级菜单里的背景颜色*/
.el-menu--inline {
  background-color: #000c17 !important;
}

.el-menu--inline .el-menu-item {
  background-color: #000c17 !important;
  padding-left: 49px !important; /*文字偏移*/
}

/*鼠标经过时改变字体颜色*/
.el-menu-item:hover {
  color: #fff !important;
}

.el-submenu__title:hover *, .el-submenu__title:hover {
  color: #fff !important;
}

/*选中的菜单背景*/
.el-menu-item.is-active {
  /*margin: 4px !important; !*边距*!*/
  background-color: #1890ff !important;
  border-radius: 5px !important; /*圆角*/
  width: calc(100% - 8px); /*相当于上面的200px - 8px */
  margin-left: 4px; /*左偏移*/
}

/* 控制图标的偏移 */
.el-menu-item.is-active i, .el-menu-item.is-active .el-tooltip {
  margin-left: -4px;
}

/*控制格子大小*/
.el-menu-item {
  height: 50px !important;
  line-height: 50px !important;
}

.el-submenu__title {
  height: 50px !important;
  line-height: 50px !important;
}

/*控制二级菜单的统一*/
.el-submenu .el-menu-item {
  min-width: 0 !important; /*最小宽度*/
}

.el-submenu .el-menu-item.is-active {
  padding-left: 45px !important;
}

/*收缩动画*/
.el-aside {
  transition: width .3s;
  box-shadow: 2px 0 6px rgba(0, 21, 41, .35);
}

.el-header {
  box-shadow: 2px 0 6px rgba(0, 21, 41, .35);
  display: flex;
  align-items: center;
}
</style>